package com.tougutips.cookbook.service.es.impl;

import com.ruoyi.common.core.domain.R;
import com.tougutips.cookbook.entity.Cookbook;
import com.tougutips.cookbook.mapper.CookbookMapperES;
import com.tougutips.cookbook.service.es.CookbookServiceES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * @author YSY
 * @Description
 * @DateTime 2022/7/21 20:16
 * @Email 296242766@qq.com
 */
@Service
public class CookbookServiceESImpl implements CookbookServiceES {
    private CookbookMapperES cookbookMapperES;

    @Autowired
    public CookbookServiceESImpl(CookbookMapperES cookbookMapperES) {
        this.cookbookMapperES = cookbookMapperES;
    }


    @Override
    public List<Cookbook> findAll() {
        LinkedList<Cookbook> cookbooks = new LinkedList<>();
        for (Cookbook cookbook : cookbookMapperES.findAll()) {
            cookbooks.add(cookbook);
        }

        return cookbooks;
    }

    @Override
    public List<Cookbook> findName(String name) {
        return cookbookMapperES.findCookbooksByCookbookNameLike(name);

    }

    @Override
    public Cookbook findById(Long id) {
        Optional<Cookbook> optional = cookbookMapperES.findById(id);
        return optional.get();
    }

    @Override
    public Cookbook addOne(Cookbook cookbook) {
        return cookbookMapperES.save(cookbook);
    }
}
